package com.gl.emms.whale.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ToggleButton;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.whale.R;
import com.gl.emms.whale.adapter.GroupAdapter;
import com.gl.emms.whale.adapter.SettingAdapter;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.app.MyApplication;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.chart.MultipleTemperatureChart;
import com.gl.emms.whale.util.GroupEntity;
import com.gl.emms.whale.util.SettingEntity;
import com.gl.emms.whale.util.SharePreferenceUtil;
import com.gl.emms.whale.util.UpdateManager;

/**设置界面类
 * @author ganlinux
 *
 */
public class SettingActivity extends WhaleMonitorActivity implements
OnClickListener{
	private ToggleButton toggleButton;
	private ListView mGroupListView ;
	private UpdateManager mUpdateManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.activity_setting);
		//这里来检测版本是否需要更新
		mUpdateManager = new UpdateManager(this);

		initViews();
	}
	/**
	 * 初始化显示
	 * @time 2014年11月12日下午1:54:17
	 * @return void
	 * @user ganlinux
	 */
	public void initViews() {
		final SharePreferenceUtil sharePreferenceUtil = new SharePreferenceUtil(this);
		toggleButton = (ToggleButton)findViewById(R.id.realtime_switch_btn);
		toggleButton.setChecked(sharePreferenceUtil.getisRealtime());
		toggleButton.setOnCheckedChangeListener(new ToggleButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				sharePreferenceUtil.setIsRealtime(toggleButton.isChecked());
			}
		});
		mGroupListView = (ListView)findViewById(R.id.ListView_Setting);
		List<SettingEntity> groupList = new ArrayList<SettingEntity>();
		SettingEntity entity = new SettingEntity(0, getString(R.string.check_update), 0);
		SettingEntity entity2 = new SettingEntity(0, getString(R.string.exit_soft),0);
		SettingEntity entity3 = new SettingEntity(0, getString(R.string.about_us),0);
		SettingEntity entity4 = new SettingEntity(0, getString(R.string.trailer),0);
		groupList.add(entity);
		groupList.add(entity2);
		groupList.add(entity3);
		groupList.add(entity4);
		SettingAdapter adapter = new SettingAdapter(this, groupList);
		mGroupListView.setAdapter(adapter);
		mGroupListView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View view,int position, long id) {
				// TODO Auto-generated method stub

				switch (position) {
				case 0://我的信息
					showChooseDownloadModeDialog();
					break;
				case 1://我的信息
					EMMSPushManager.destory(SettingActivity.this);
					MyApplication.getInstance().exit();				
					break;
				case 2://我的信息
					Intent aboutSoftActivity = new Intent(SettingActivity.this,AboutSoftActivity.class);  
					startActivity(aboutSoftActivity);
					break;
				case 3://宣传片
					Intent playerActivity = new Intent(SettingActivity.this,PlayerActivity.class);  
					startActivity(playerActivity);
					break;
				default:
					break;
				}


			}
		});
		Button titleBackBtn = (Button)findViewById(R.id.TitleBackBtn);  
		titleBackBtn.setOnClickListener(this);

		setListViewHeightBasedOnChildren(mGroupListView);
	}
	public void setListViewHeightBasedOnChildren(ListView listView) {   
		// 获取ListView对应的Adapter   
		ListAdapter listAdapter = listView.getAdapter();   
		if (listAdapter == null) {   
			return;   
		}   

		int totalHeight = 0;   
		for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   
			// listAdapter.getCount()返回数据项的数目   
			View listItem = listAdapter.getView(i, null, listView);   
			// 计算子项View 的宽高   
			listItem.measure(0, 0);    
			// 统计所有子项的总高度   
			totalHeight += listItem.getMeasuredHeight();    
		}   

		ViewGroup.LayoutParams params = listView.getLayoutParams();   
		params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));   
		// listView.getDividerHeight()获取子项间分隔符占用的高度   
		// params.height最后得到整个ListView完整显示需要的高度   
		listView.setLayoutParams(params);   
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.TitleBackBtn:
			new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK);
			finish();
			break;
		default:
			break;
		}
	}
	private void showChooseDownloadModeDialog(){
		Dialog noticeDialog;
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle(getString(R.string.update_alert_title));
		builder.setMessage(getString(R.string.update_alert_message));
		builder.setPositiveButton(getString(R.string.soft_download), new AlertDialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				mUpdateManager.checkUpdateInfo();			
			}

		});
		builder.setNegativeButton(getString(R.string.brower_download), new AlertDialog.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				Uri uri = Uri.parse(Constant.DOWNLOAD_URL);  
				startActivity(new Intent(Intent.ACTION_VIEW,uri));
			}

		});
		noticeDialog = builder.create();
		noticeDialog.show();
	}
}